package com.iot.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.iot.util.DataHelper;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

public class JsonHelper {
    private static ValueFilter filter = new ValueFilter() {

        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
    };

    /**
     * 把类对象转化成json string
     *
     * @param t
     * @return
     */
    public static <T> String toJson(T t) {
        //JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";//设置日期格式
        try {
            return JSONObject.toJSONString(t, filter, SerializerFeature.WriteMapNullValue , SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            //   Log.e("数据转换出错", "exception:" + e.getMessage());
        }
        return "";
    }

    public static <T> T toBean(String jsonString, Class<T> clszz) {
        try {
            if (jsonString != null && !"".equals(jsonString.trim())) {
                T res = JSONArray.parseObject(jsonString.trim(), clszz);
                return res;
            }
        } catch (Exception e) {
            System.out.println("fastjson" + e.getMessage());
        }
        return null;
    }

    public static <T> T toPojo(String jsonString, Class<T> clszz) {
        try {
            if (jsonString != null && !"".equals(jsonString.trim())) {
                T res = JSONObject.parseObject(jsonString.trim(), clszz);
                return res;
            }
        } catch (Exception e) {
            System.out.println("fastjson" + e.getMessage());
        }
        return null;
    }
    public static <T> T toEntity(String jsonString, Class<T> clszz) {
        try {
            T bean = clszz.newInstance();
            if (jsonString != null && !"".equals(jsonString.trim())) {
                Map<String, Object> map = toMap(jsonString.trim());
                Field[] fields = clszz.getDeclaredFields();
                for (Field field : fields) {
                    Object obj = map.get(field.getName());
                    if (obj == null || "null".equalsIgnoreCase(obj.toString())) {
                        continue;
                    }
                    field.setAccessible(true);
                    field.set(bean, DataHelper.ConvertValue(field.getType(), obj.toString()));
                }
                return bean;
            }
        } catch (Exception e) {
            System.out.println("fastjson" + e.getMessage());
        }
        return null;
    }

    public static <T> List<T> toList(String gsonString, Class<T> clszz) {
        List<T> list = null;
        try {
            if (gsonString != null && !"".equals(gsonString.trim())) {
                list = JSONArray.parseArray(gsonString.trim(), clszz);
            }
        } catch (Exception e) {
            //   Log.e("数据转换出错", "exception:" + e.getMessage());
        }
        return list;
    }

    public static <T> List<T> toList(String gsonString, String key, Class<T> clszz) {
        List<T> list = null;
        try {
            if (gsonString != null && !"".equals(gsonString.trim())) {
                if ("".equals(key.trim())) {
                    list = JSONArray.parseArray(gsonString.trim(), clszz);
                } else {
                    JSONObject jsonObj = JSON.parseObject(gsonString);
                    JSONArray result = jsonObj.getJSONArray(key);
                    list = JSONArray.parseArray(result.toJSONString().trim(), clszz);
                }
            }
        } catch (Exception e) {
            //  Log.e("数据转换出错", "exception:" + e.getMessage());
        }
        return list;
    }

    /**
     * 功能描述：把JSON数据转换成较为复杂的java对象列表
     *
     * @param jsonString JSON数据
     * @return
     * @throws Exception
     * @author myclover
     */
    public static List<Map<String, Object>> toMapList(String jsonString) {
        try {
            List<Map<String, Object>> map = JSON.parseObject(jsonString,
                    new TypeReference<List<Map<String, Object>>>() {
                    });
            return map;

        } catch (Exception e) {
            //  Log.e("数据转换出错", "exception:" + e.getMessage());
        }
        return null;
    }

    public static List<Map<String, String>> toMapList(String jsonString, String key) {
        try {

            JSONObject jsonObj = JSON.parseObject(jsonString);
            JSONArray result = jsonObj.getJSONArray(key);
            //list = JSONArray.parseArray(result.toJSONString().trim(), clszz);

            List<Map<String, String>> map = JSON.parseObject(result.toJSONString().trim(),
                    new TypeReference<List<Map<String, String>>>() {
                    });
            return map;

        } catch (Exception e) {
            //  Log.e("数据转换出错", "exception:" + e.getMessage());
        }
        return null;
    }

    /**
     * 将json转化成map
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, Object> toMap(String jsonStr) {

        Map<String, Object> map = JSON.parseObject(
                jsonStr, new TypeReference<Map<String, Object>>() {
                });

        return map;
    }
}
